package com.fastadmin.mapper.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.fastadmin.pojo.Company;
import com.fastadmin.pojo.FastParam;
import com.xphsc.easyjdbc.EasyJdbcTemplate;
import com.xphsc.easyjdbc.page.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.xphsc.easyjdbc.core.metadata.ElementResolver;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;

@Repository
@Slf4j
public class CompanyMapper extends BaseMapper {
    @Autowired
    EasyJdbcTemplate easyJdbcTemplate;

    public PageInfo<Company> findByFastParam(FastParam fastParam) {
        String tableName = ElementResolver.resolve(Company.class).getTable();
        PageInfo<Company> pageInfo = this.easyJdbcTemplate.findByPage(fastParam.getSql(tableName), Company.class, fastParam.getOffset(), fastParam.getLimit());
        return pageInfo;
    }

    public Company findById(Long ids) {
        Company company = this.easyJdbcTemplate.getByPrimaryKey(Company.class, ids);
        return company;
    }

    public void deleteById(String id) {
        this.easyJdbcTemplate.deleteByPrimaryKey(Company.class, id);
    }

    public void update(Company company) {
        company.setUpdate_time(new Timestamp(System.currentTimeMillis()));
        this.easyJdbcTemplate.update(company);
    }

    public Object save(Company company) {
        company.setCreate_time(new Timestamp(System.currentTimeMillis()));
        company.setUpdate_time(new Timestamp(System.currentTimeMillis()));
        return this.easyJdbcTemplate.insertKey(company);

    }

    public List<Company> findByIds(String ids) {
        if (StrUtil.isBlank(ids)) {
            return CollUtil.newArrayList();
        }
        return this.easyJdbcTemplate.findByIds(Company.class, Arrays.asList(StrUtil.splitToArray(ids, ',')));

    }

    public List<Company>  findAll() {
        List<Company> list = this.easyJdbcTemplate.findAll(Company.class);
        return list;
    }
}
